package com.example.minefieldkeepercloud.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 日志adapter
 * Created by Administrator on 2019/3/5.
 */

public class LogAdapter extends BaseAdapter {

    private List<String> list;
    private Activity context;


    public LogAdapter(List<String> list, Activity context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 添加单挑数据
     * @param s
     */
    public void additem(String s)
    {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        String format = df.format(date);
        list.add(0,format+"  "+s);

        notifyDataSetChanged();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView==null)
        {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_log,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);

        }else
        {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(list.get(i));
        return convertView;
    }
    class ViewHolder
    {
        TextView textView;

        public ViewHolder(View view ) {
            textView=view.findViewById(R.id.tv_content);
        }
    }
}
